from django.db import models
from django.contrib.auth.models import User
import datetime
from django.conf import settings
from tagging.fields import TagField
from markdown import markdown

# Create your models here.
class Category(models.Model):
    title = models.CharField(max_length=250)
    slug = models.SlugField(unique=True)
    description = models.TextField()
    
    class Meta:
        verbose_name_plural = "Categories"
        ordering = ['title']
    
    def __unicode__(self):
        return self.title
    
    def get_absolute_url(self):
        return "/categories/%s/" % self.slug


class Entry(models.Model):
    LIVE_STATUS = 1
    DRAFT_STATUS = 2
    STATUS_CHOICES = (
        (LIVE_STATUS, 'Live'),
        (DRAFT_STATUS, 'Draft'),
    )
    
    #Core fields
    title = models.CharField(max_length=250)
    excerpt = models.TextField(blank=True)
    body = models.TextField()
    pub_date = models.DateTimeField(default=datetime.datetime.now)
    
    # Fields to store generated HTML.
    excerpt_html = models.TextField(editable=False, blank=True)
    body_html = models.TextField(editable=False, blank=True)
    
    # Matedata
    author = models.ForeignKey(User)
    enable_comments = models.BooleanField(default=True)
    featured = models.BooleanField(default=False)
    slug = models.SlugField(unique_for_date='pub_date')
    status = models.IntegerField(choices=STATUS_CHOICES, default=LIVE_STATUS)

    #Categorization
    categories = models.ManyToManyField(Category)
    tags = TagField()

    class Meta:
        ordering = ['-pub_date']
        verbose_name_plural = "Entries"    
    
    def __unicode__(self):
        return self.title

    def save(self):
        self.body_html = markdown(self.body)
        if self.excerpt:
            self.excerpt_html = markdown(self.excerpt)
        super(Entry, self).save()
    
    def get_absolute_url(self):
        return "/zblog/%s/%s/" % (self.pub_date.strftime("%Y/%b/%d").lower(), self.slug)
    
    @property
    def comments_enabled(self):
        delta = datetime.datetime.now() - self.pub_date
        return delta.days < 60
    

class Link(models.Model):
    title = models.CharField(max_length=250)
    description = models.TextField(blank=True)
    description_html = models.TextField(blank=True)
    url = models.URLField(unique=True)
    
    posted_by = models.ForeignKey(User)
    pub_date = models.DateTimeField(default=datetime.datetime.now)
    slug = models.SlugField(unique_for_date='pub_date')
    
    tags = TagField()
    enable_comments = models.BooleanField(default=True)
    post_elsewhere= models.BooleanField(default=True)
    
    class Meta:
        ordering = ['-pub_date']
    
    def __unicode__(self):
        return self.title
    
    def save(self):
        if self.description:
            self.description_html = markdown(self.description)
        super(Link, self).save()
        
    def get_absolute_url(self):
        return "/links/%s/%s/" % (self.pub_date.strftime("%Y/%b/%d").lower(), self.slug)
 
